<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Promise/Deferred 模式</title>
  </head>
  <body>
  </body>
  <script src="./jquery.js"></script>
  <script>
    alert(1221)
    function request(option){
        var def=$.Deferred()//调用了jQuery的 Deferred 方法
        if(!option || !option.url){
            throw Error('url is undefined.')
        }
        var _config={
            type:'get',
            dataType:'JSON',
            success:function(res){
                def.resolve(res)///通过resolve 来改变def 对象的状态为成功
            },
            error:function(a,b,err){
                def.reject(err);///通过reject来改变对象def对象的状态为失败
            }
        };
        $.extend(_config,option,true);
        $.ajax(_config);
        return def.promise();
    }

    //request 调用

    var option={
        url:'./city.json'
    };

    request(option).then(function(data){
        console.log(data)
    },function(err){
        console.log(err);
    })
  </script>
</html>
